/**
 * 客户资源录入脚本支持
 */
var clientKey = null;
var modifydata = null;
var date = "";
var time = "";
var region = "";
var modifyKey = "";
var studentKey = "";

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 初始化
	button_bind();

	client_detailed( $( "#tbl_client" ), false );
	client_detailed( $( "#tbl_reserve" ), false );
	client_detailed( $( "#tbl_reserve" ), false, "hide2" );
	client_detailed( $( "#tbl_reserve_log" ), false );

	// 绑定学员点击事件
	init_table_sh_click( $( "#tbl_reserve_log" ) );
	init_table_sh_click( $( "#tbl_reserve" ) );

	// 修改数据设置
	if( modifydata && modifydata.key )
	{
		setModifyData( modifydata );
		search_reserve_log( clientKey );
		client_detailed( $( "#tbl_reserve" ), true );
	}
} );

/**
 * 初始化以fields为样式名的标题栏具备隐藏和显示其它表格行的功能
 */
function init_table_sh_click( tbl )
{
	var th = tbl.find( "tr[class=fields] th" );

	th.css( "cursor", "pointer" );
	th.bind( "click", function()
	{
		var trs = $( this ).parent().parent().find( "tr[class!=fields]" );
		trs.eq( 0 ).css( "display" ) == "none" ? trs.show() : trs.hide();
	} );
}

//--------------------------------------------------------------------------
//
//	consultant_client.php
//
//--------------------------------------------------------------------------

function button_bind()
{
	$( "#btnSelect" ).bind( "click", function()
	{
		parent.clienter.setCallback( clientSelectedHandler );
		parent.clienter.reset();
		parent.clienter.open();
	} );

	$( "#btn_detailed" ).click( function( e )
	{
		e.stopPropagation();

		var show = $( "#tbl_client" ).find( "tr[rel='hide']" ).eq( 0 ).visible();
		client_detailed( $( "#tbl_client" ), !show );
	} );

	$( "#btn_students" ).click( function( e )
	{
		e.stopPropagation();

		var show = $( "#tbl_student" ).find( "tr[rel='hide']" ).eq( 0 ).visible();
		client_detailed( $( "#tbl_student" ), !show );
	} );

	$( "#non" ).change( function()
	{
		var is_non = $( this ).prop( "checked" );
		client_detailed( $( "#tbl_reserve" ), is_non, "hide2" );

		if( !is_non )
		{
			$( "#tbl_student" ).show();
			bind_st_click( true );
		}
		else
		{
			$( "#tbl_student" ).hide();
			bind_st_click( false );
		}
	} );

	$( "#date" ).change( function()
	{
		date = $( this ).val();
		$( "#receptionlst ul" ).empty();
	} );

	$( "#time" ).change( function()
	{
		time = $( this ).val();
		$( "#receptionlst ul" ).empty();
	} );

	$( "#btnSearchReception" ).click( function( e )
	{
		e.stopPropagation();
		search_reception();
	} );

	$( "#btnRestoryReception" ).click( function( e )
	{
		e.stopPropagation();
		restory_reception();
	} );
}

function client_detailed( tar, visible, rel )
{
	var r = "hide";

	if( !empty( rel ) )
		r = rel;

	if( visible )
		tar.find( "tr[rel='" + r + "']" ).show();
	else
		tar.find( "tr[rel='" + r + "']" ).hide();

	setTableRowBackColor( tar );
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function setClientData( data )
{
	// 客户资料
	if( typeof ( data ) == "object" && data.nickname )
	{
		clientKey = data.key;

		$( "#nickname" ).html(
				data.nickname
						+ ( data.invalid == 1 ? "<em class='caption'>（无效客户）</em>"
								: "<em class='valid'>（有效客户）</em>" ) );
		$( "#category" ).html( shtml( data.categoryns ) );
		$( "#source" ).html( shtml( data.sourcens ) );
		$( "#keyword" ).html( shtml( data.keyword ) );
		$( "#address" ).html( data.province + " " + data.city + " " + data.address );
		$( "#phone" ).html( shtml( data.phone ) );
		$( "#email" ).html( shtml( data.email ) );
		$( "#qq" ).html( shtml( data.qq ) );
		$( "#ip" ).html( shtml( data.ip ) );
	}
	else
	{
		clientKey = null;

		$( "#nickname" ).html( "&nbsp;" );
		$( "#category" ).html( "&nbsp;" );
		$( "#source" ).html( "&nbsp;" );
		$( "#address" ).html( "&nbsp;" );
		$( "#keyword" ).html( "&nbsp;" );
		$( "#address" ).html( "&nbsp;" );
		$( "#phone" ).html( "&nbsp;" );
		$( "#email" ).html( "&nbsp;" );
		$( "#qq" ).html( "&nbsp;" );
		$( "#ip" ).html( "&nbsp;" );
	}
}

function shtml( val )
{
	if( empty( val ) )
		return "<em class='caption'>未填写</em>";
	else if( val == '--' )
		return "<em class='caption'>禁止查看</em>";
	return val;
}

function request_student( key )
{
	submitter.post( "student_search.php", { key: key }, function( d )
	{
		if( d.err == 0 )
		{
			$( "#studentlst ul" ).empty();

			var size = parseInt( d[ 'size' ] );
			for( var i = 0; i < size; i++ )
			{
				var v = d[ i ];
				insert_student( v );
			}
		}
		else
		{
			infopad.err( d.msg );
		}
	}, "json" );
}

function search_reserve_log( clientKey )
{
	submitter.post( "client_reserve_log_search.php", { key: clientKey }, function( d )
	{
		if( d.err == 0 )
		{
			var size = parseInt( d[ 'size' ] );
			size > 0 && $( "#loglist ul" ).empty();

			for( var i = 0; i < size; i++ )
			{
				insert_reserve_log( d[ i ] );
			}
		}
		else
		{
			infopad.err( d.msg );
		}
	}, "json" );
}

function insert_reserve_log( d )
{
	var h = "<li class=\"rect\">";
	h += "<span><b>操作人：</b><label class=\"stress\">" + d.modifyer + "</label></span>";
	h += "<span style='padding-left:20px;'><b>操作时间：</b>" + d.date + "</span>";
	h += "<p class=\"line\"></p>";
	h += "<p><b>状态：</b><span class='unit hand " + state_style( d.state ) + "'>" + d.statens + "</span></p>";
	h += "<p><b>类型：</b>" + d.type + "</p>";
	h += "<p>" + d.remark + "</p>";
	h += "</li>";

	$( "#loglist ul" ).append( h );
}

function state_style( s )
{
	switch( parseInt( s ) ) {
		case 1:
			return 'confirmed';
		case 2:
			return 'completed';
		case 3:
			return 'expired';
		default:
			return '';
	}
}

function search_reception()
{
	var date = $( "#date" ).val();
	var time = $( "#time" ).val();

	submitter.post( "consultant_reserve_user_search.php", { key: modifyKey, date: date, time: time,
		region: region }, function( d )
	{
		if( d.err == 0 )
		{
			var size = parseInt( d[ 'size' ] );

			size > 0 && $( "#receptionlst ul" ).empty();

			for( var i = 0; i < size; i++ )
			{
				var v = d[ i ];
				insert_reception_user( v );
			}

			// 重新绑定选定事件
			$( "#receptionlst ul li" ).click( function()
			{
				$( this ).parent().find( "li" ).each( function()
				{
					$( this ).removeClass( "selected" );
				} );
				$( this ).toggleClass( "selected" );
			} );
		}
		else
		{
			infopad.err( d.msg );
		}
	}, "json" );
}

function restory_reception()
{
	// 还原数据设置
	if( modifydata && modifydata.key )
	{
		var d = modifydata;
		date = d.date;
		time = d.time;
		region = d.region;
		reception_u = d.reception_u;

		$( "#date" ).val( date );
		$( "#time" ).val( time );

		// 直接选定已经设置好的时间，随意更改，等提交时，再检测这个时间段内该咨询师是否有别的接待安排
		$( "#needtime" ).val( d.needtime );

		// 设置当前接待人
		$( "#receptionlst ul" ).empty();
		insert_reception_user( { username: d.ur_username, nickname: d.receptionor, jobns: d.jobns } );
		$( "#receptionlst ul" ).find( "li#" + d.ur_username ).addClass( "selected" );
	}
}

function insert_reception_user( d )
{
	var h = "<li id=\"" + d.username + "\" class=\"unit\">";
	h += "<label class=\"stress\">" + d.nickname + "</label>";
	h += "，" + d.jobns;
	h += "</li>";

	$( "#receptionlst ul" ).append( h );
}

/**
 * 根据数据参数添加一条学员数据
 * 
 * @param d 数组
 */
function insert_student( d )
{
	var h = "<li rel=\"" + d.key + "\">";

	h += "<p class='title'><span>" + d.nickname + "</span></p>";
	h += "<p><b>性别</b><span>" + d.sex + "</span></p>";
	h += "<p><b>年龄</b><span>" + d.age + " 岁</span></p>";
	h += "<p><b>生日</b><span>" + ( empty( d.birthday ) ? "<em class='caption'>未填写</em>" : d.birthday )
			+ "</span></p>";
	h += "<p><b>学校</b><span>" + ( d.school == null ? "<em class='caption'>未填写</em>" : d.school )
			+ "</span></p>";
	h += "<p><b>学员特征</b></p>";
	h += "<div class=\"content\">" + d.feature + "</div>";
	h += "</li>";

	$( "#studentlst ul" ).append( h );

	// 根据数据选定已经保存的选择
	if( studentKey.indexOf( d.key ) != -1 )
		$( "#studentlst ul li:last" ).attr( "class", "selected" );
}

function empty_student()
{
	$( "#studentlst ul" ).empty();
	$( "#studentlst ul" ).append( "<li>暂无学员资料，不能进行预约。</li>" );
}

function setModifyData( d )
{
	setClientData( d );

	// clientKey 放在 setClientData 之后，覆盖方法内赋值的内容
	modifyKey = d.key;
	clientKey = d.client;
	studentKey = d.student;
	date = d.date;
	time = d.time;
	region = d.region;
	reception_u = d.reception_u;

	// 设置当前接待人
	$( "#receptionlst ul" ).empty();
	insert_reception_user( { username: d.ur_username, nickname: d.receptionor, jobns: d.jobns } );
	$( "#receptionlst ul" ).find( "li#" + d.ur_username ).addClass( "selected" );

	if( clientKey != null )
		request_student( clientKey );

	$( "#datetime" ).html( "<b>日期：</b>" + date + "<br><b>时间：</b>" + time );
	$( "#remark" ).html( d.remark );
	$( "#sms" ).html( ( d.sms == 1 ? "已" : "未" ) + "发送短信" );

	// 测评类型，自由更改
	$( "#non" ).html( d.non == 1 ? "非测评类型" : "测评类型" );

	if( d.non == 1 )
	{
		client_detailed( $( "#tbl_student" ), false );
		client_detailed( $( "#tbl_reserve" ), true, "hide2" );
		$( "#tbl_student" ).hide();

		$( "#notice" ).html( d.notice );
	}

	$( "#regionbx" ).find( "span" ).remove();
	$( "#regionbx" ).prepend( "<span class=\"stress unit hand\">" + d.regionns + "</span>" );

	// 直接选定已经设置好的时间，随意更改，等提交时，再检测这个时间段内该咨询师是否有别的接待安排
	$( "#needtime" ).html( d.needtime + "小时" );
}
